<?php
    /**
    * Module.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    /**
    * This is the base class for all application modules. This means all
    * modules have some sort of Presenter, authentication and an open log file.
    */
    abstract class AT_Module extends AT_Object
    {
        /**
        * Specifies whichview to use for the module. The default is
        * none.
        * @var string
        */
        private $view;
        
        /**
        * Determines which authentication method is used. By default no
        * authentication is required to access the module.
        */
        private $auth;
        
        /**
        * Array of data the module collects and then passes to the view.
        * @var array
        */
        private $data = array();
        
        /**
        * Stores the name of this class.
        * @var string
        */
        private $name;
        
        /**
        * The name of the template file. Defaults to the name of the class
        * in the AT_Module constructor.
        * @var string
        */
        private $tplFile;
        
        /**
        * The name of the module.
        * @var string
        */
        private $moduleName;
        
        /**
        * The name of the template file we want our presenter to use. Leave
        * NULL or empty for the default template.
        * @var string
        */
        private $pageTemplateFile;
        
        /**
        * Sets the default name and tplFile for the current Module.
        */
        public function __construct($moduleName)
        {
            parent::__construct();
            
            $this->name = $this->me->getName();
            $this->moduleName = $moduleName;
            $this->tplFile = $this->name.'.tpl';
            $this->pageTemplateFile = NULL;
            
            $this->view = AT_View::factory('none', $this);
            $this->auth = AT_Auth::factory('none');
        }
        
        /**
        * The __default() method is ran by the controller if no particular
        * event was specified.
        * @return void
        */
        public abstract function __default();
        
        /**
        * Sets the authorization method this module should use.
        * @param AT_Auth $auth The authorization type.
        * @return void
        */
        public function setAuth(AT_Auth $auth)
        {
            $this->authType = $auth;
        }
        
        /**
        * Sets the presenter for this module.
        * @param AT_View $presenter The view to use for this module.
        * @return void
        */
        public function setView(AT_View $view)
        {
            $this->view = $view;
        }
        
        /**
        * Sets the page template file.
        * @param string $ptf The filename of the template.
        * @return void
        */
        public function setPageTemplateFile($ptf)
        {
            $this->pageTemplateFile = $ptf;
        }
        
        /**
        * Sets the template file that represents this module's data.
        * @param string $tpl The filename of the file.
        * @return void
        */
        public function setTplFile($tpl)
        {
            $this->tplFile = $tpl;
        }
        
        /**
        * Returns the authorization type this module uses.
        * @return AT_Auth
        */
        public function getAuth()
        {
            return $this->auth;
        }
        
        /**
        * Returns the presenter being used by this module.
        * @return AT_View
        */
        public function getView()
        {
            return $this->view;
        }
        
        /**
        * Returns the template file the presenter is supposed to use.
        * @return string
        */
        public function getPageTemplateFile()
        {
            return $this->pageTemplateFile;
        }
        
        /**
        * Returns the template file the module uses to present its data.
        * @return string
        */
        public function getTplFile()
        {
            return $this->tplFile;
        }
        
        /**
        * Returns the name of the module this application class belongs to.
        * @return string
        */
        public function getModuleName()
        {
            return $this->moduleName;
        }
        
        /**
        * Sets module data in $this->data. This data is eventually passed to
        * the presenter.
        * @param string $var Name of variable.
        * @param mixed $val Data to save.
        * @return void
        */
        public function set($var, $val)
        {
            $this->data[$var] = $val;
        }
        
        /**
        * Returns the data stored by this module.
        * @return array The array of internal data.
        */
        public function getData()
        {
            return $this->data;
        }
        
        /**
        * Determines if $module is a valid module and that it fits within the
        * framework. A module is valid if it is a child of AT_Module and
        * AT_Auth (which is a child of AT_Module).
        * @return boolean Returns true if valid, false otherwise.
        */
        public static function isValid($module)
        {
            return is_object($module) && $module instanceof AT_Module;
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
